package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AlunoDAO;
import entity.Aluno;

@WebServlet(name = "AlunoController", urlPatterns = { "/alunoInsert",
		"/alunoUpdate", "/alunoDelete", "/alunoList", })
public class AlunoController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public AlunoController() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		List<Aluno> alunos = null;

		switch (action) {

		case "confirmDelete":
			String par = request.getParameter("matricula");
			request.setAttribute("matricula", par);
			request.getRequestDispatcher(
					"/view/private/aluno/confirmDelete.jsp").forward(request,
					response);
			break;

		case "list":
			listAluno(request, response);

			break;

		case "altera":

			alunos = AlunoDAO.findByMatricula(new Integer(request
					.getParameter("matricula")));
			request.setAttribute("lista", alunos);
			request.getRequestDispatcher("/view/private/aluno/altera.jsp")
					.forward(request, response);

			break;

		case "delete":

			if (AlunoDAO.delete(new Integer(request.getParameter("matricula")))) {
				response.sendRedirect("" + request.getContextPath()
						+ "/alunoList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/alunoList?action=list");
			}
			break;

		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		Aluno aluno = new Aluno();

		switch (action) {
		case "insert":

			aluno.setNome(request.getParameter("nome"));
			aluno.setCpf(request.getParameter("cpf"));
			aluno.setEndereco(request.getParameter("endereco"));
			aluno.setEmail(request.getParameter("email"));
			aluno.setTelefone(request.getParameter("telefone"));

			if (AlunoDAO.insert(aluno)) {
				response.sendRedirect("" + request.getContextPath()
						+ "/alunoList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/msg_erro.jsp");
			}

			break;

		case "update":

			aluno.setMatricula(new Integer(request.getParameter("id")));
			aluno.setCpf(request.getParameter("cpf"));
			aluno.setNome(request.getParameter("nome"));
			aluno.setEndereco(request.getParameter("endereco"));
			aluno.setEmail(request.getParameter("email"));
			aluno.setTelefone(request.getParameter("telefone"));

			if (AlunoDAO.update(aluno)) {
				response.sendRedirect("" + request.getContextPath()
						+ "/alunoList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/alunoList?action=list");
			}

			break;
		}
	}

	private void listAluno(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Aluno> alunos = AlunoDAO.getAll();
		alunos = AlunoDAO.getAll();
		request.setAttribute("lista", alunos);
		
		Integer totalLogados = (Integer) getServletContext().getAttribute(
				"qde_Users");
		request.setAttribute("totalLogados", totalLogados);
		
		request.getRequestDispatcher("/view/private/aluno/lista.jsp").forward(
				request, response);

	}
}
